C++题目:char s[]={'h','e','l','l','o'}; strlen(s)值为?

来源:百度知道 编辑:UC知道 时间:2024/05/27 14:39:59

他的值应该是不确定的,因为你没有在字符串s中加入字符串结束符,如果你在初始化s时变为char s[]={'h','e','l','l','o','\0'}; 那么,他的值应该就是5.

#include <iostream.h>
#include <string>
void main()
{
char s[] = {'h','e','l','l','o'};

int a = strlen(s); //值为 11
int b = sizeof(s); //值为 5
cout <<a <<b;

}

那个值是不确定的值,运气好的话可能是5(概率非常低),一般情况是大于5的,要能得到确定的值,得这样改:char s[] = {'h', 'e', 'l', 'l', 'o', '\0'};

长度不确定,因为strlen求长度以\0为结尾,这里没有结尾符号,所以长度违法确定

不知道,因为没有'\0'字符串长度无法计算

5